package com.yzy.controller;

import com.yzy.annotation.UserLoginToken;
import com.yzy.entity.File;
import com.yzy.entity.Goods;
import com.yzy.entity.query.WrappedGood;
import com.yzy.service.FileService;
import com.yzy.service.GoodsService;
import com.yzy.util.FileUtil;
import com.yzy.util.Message;
import com.yzy.util.ResponseCode;
import com.yzy.util.TokenUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    private GoodsService goodsService;

    @Resource
    private FileService fileService;

    @Resource
    private FileUtil fileUtil;

    @GetMapping("/all")
    public Object getAll(){
        List<Goods> list = goodsService.list();
        Map<String,List<Goods>> map = new HashMap<>();
        map.put("data",list);
        return map;

    }


    @GetMapping("/search")
    public Object search(@RequestParam("key") String key){
        List<WrappedGood> list = goodsService.search(key);
        return ResponseCode.buildSuccess(list);
    }

    /**
     * @description 用户创建商品
     * @param name 商品名称
     * @param introduction 商品介绍
     * @param price 商品单价
     * @param file 文件
     * @return java.lang.Object
     * @author mgl
     * @date 2023/7/26 19:44
     */
    @PostMapping("/create")
    @UserLoginToken
    public Object createOrder(@RequestParam("name") String name,
                              @RequestParam("introduction") String introduction,
                              @RequestParam("price") Double price,
                              MultipartFile file){

        //先创建文件
        File file1 = null;
        try {
            file1 = fileUtil.transferDocumentFile(file);
        }catch (Exception e) {
            return ResponseCode.buildFailed(e.getMessage());
        }

        //在文件实体类中设置上传用户id，然后储存
        file1.setUploadUserId(Integer.valueOf(TokenUtil.getTokenUserId()));
        fileService.save(file1);

        //然后存储商品信息
        Goods goods = new Goods();
        goods.setName(name);
        goods.setIntroduction(introduction);
        goods.setPrice(price);
        goods.setUploadUserId(Integer.valueOf(TokenUtil.getTokenUserId()));
        goods.setFileId(file1.getId());
        goodsService.save(goods);
        return ResponseCode.buildSuccess(Message.GOODS_CREATE_SUCCESS);
    }

    /**
     * @description 查询商品详情
     * @param goodsId  商品id
     * @return java.lang.Object
     * @author mgl
     * @date 2023/8/12 10:32
     */
    @GetMapping("/details")
    public Object details(@RequestParam("goodsId") Integer goodsId){
        return ResponseCode.buildSuccess(goodsService.details(goodsId));
    }

    /**
     * @description 对应我的上传
     * @return java.lang.Object
     * @author mgl
     * @date 2023/8/12 11:48
     */
    @UserLoginToken
    @GetMapping("/goods/mine")
    public Object mine(){
        Integer userId = Integer.valueOf(TokenUtil.getTokenUserId());
        return ResponseCode.buildSuccess(goodsService.mine(userId));
    }
}
